package com.gomore.opple.module.order.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.rest.order.OrderBill;
import com.gomore.opple.service.ChangeOrderStateClickListener;
import com.gomore.opple.utils.DensityUtil;
import com.gomore.opple.utils.DialogUtils;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;
import com.gomore.opple.web.cgform.order.entity.TOOrderEntity;
import com.gomore.opple.widgets.XCRoundRectImageView;
import com.gomore.opple.widgets.adapter.CommonAdapter;
import com.gomore.opple.widgets.adapter.ViewHolder;
import com.gomore.opple.widgets.dialog.ConfirmDialog;

import java.util.List;

/**
 * Created by asus on 2017/6/2.
 */
public class OrderAdapter extends CommonAdapter<OrderBill> {
    Activity mactivity;
    Context mContext;
    OrderBill morderBill;
    private ChangeOrderStateClickListener mchangeOrderStateClickListener;

    public void setChangeOrderStateClickListener(ChangeOrderStateClickListener changeOrderStateClickListener) {
        mchangeOrderStateClickListener = changeOrderStateClickListener;
    }

    public OrderAdapter(Activity activity, Context context, int layoutId, List<OrderBill> messageList) {
        super(context, layoutId, messageList);
        mactivity = activity;
        mContext = context;
    }

    @Override
    protected void convert(ViewHolder holder, final OrderBill orderBill, final int position) {
        morderBill = orderBill;
        final TOOrderEntity toOrderEntity = orderBill.getOrder();
        if (toOrderEntity == null) {
            return;
        }
        if (toOrderEntity.getOrderNumber() != null) {
            holder.setText(R.id.order_number, toOrderEntity.getOrderNumber());
        }
        TextView order_state = holder.getView(R.id.order_state);
        if (toOrderEntity.getOrderState() != null) {
            order_state.setText(GlobalConstant.OrderState.getStateNameByCode(toOrderEntity.getOrderState()));
            order_state.setTextColor(mactivity.getResources().getColor(GlobalConstant.OrderState.getColorByCode(toOrderEntity.getOrderState())));
        }
        XCRoundRectImageView costomer_icon = holder.getView(R.id.costomer_icon);
        TOConsumerEntity toConsumerEntity = orderBill.getConsumer();
        int height = DensityUtil.dip2px(mactivity, 65);
        String url = "demo";
        if (toConsumerEntity != null && toConsumerEntity.getPicture() != null) {
            url = toConsumerEntity.getPicture();
        }
        Glide.with(mactivity)
                .load(url)
                .asBitmap()
                .centerCrop()
                .override(height, height)
                .placeholder(R.mipmap.consumer_icon)
                .into(costomer_icon);
        if (toOrderEntity.getReceiverName() != null) {
            holder.setText(R.id.costomer_name, toOrderEntity.getReceiverName());
        }
        if (toOrderEntity.getReceiverMobile() != null) {
            holder.setText(R.id.costomer_mobile, toOrderEntity.getReceiverMobile());
        }
        if (toOrderEntity.getReceiverAddress() != null) {
            holder.setText(R.id.costomer_address, toOrderEntity.getReceiverAddress());
        }
        if (toOrderEntity.getGoodsNum() != null) {
            holder.setText(R.id.good_number, "共" + toOrderEntity.getGoodsNum() + "件商品");
        }
        if (toOrderEntity.getOrderPrice() != null) {
            holder.setText(R.id.total, "¥" + toOrderEntity.getOrderPrice().toString());
        }
        LinearLayout choice = holder.getView(R.id.choice);
        if (toOrderEntity.getOrderState().equals(GlobalConstant.OrderState.UNPAID)) {
            choice.setVisibility(View.VISIBLE);
        } else {
            choice.setVisibility(View.GONE);
        }
        TextView delete_order = holder.getView(R.id.delete_order);
        TextView pay_order = holder.getView(R.id.pay_order);
        delete_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //确定框
                ConfirmDialog.onNoOnclickListener onNoOnclickListener = null;
                ConfirmDialog.onYesOnclickListener onYesOnclickListener = null;
                final String title = "提示";
                String message = "确定撤销该订单吗?";
                onYesOnclickListener = new ConfirmDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        mchangeOrderStateClickListener.changeOrderState(toOrderEntity.getOrderNumber());
                    }
                };
                onNoOnclickListener = new ConfirmDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        DialogUtils.cancel();
                    }
                };
                DialogUtils.showConfirmDialog(mactivity, onYesOnclickListener, onNoOnclickListener, title, message);
            }
        });
        pay_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentStart.getInstance().startOrderPayActivity(mactivity, toOrderEntity.getOrderNumber(), toOrderEntity.getOrderState());
            }
        });
    }
}